<template>
    <div class="register-page">
        <div class="register-card">
            <div class="register-title">创建账户</div>
            <el-form ref="formRef" :model="form" :rules="rules" label-position="top" class="register-form">
                <el-form-item label="账号" prop="username">
                    <el-input v-model="form.username" placeholder="请输入账号" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" placeholder="请输入密码" show-password />
                </el-form-item>
                <el-form-item label="再次输入密码" prop="confirmPassword">
                    <el-input v-model="form.confirmPassword" placeholder="请再次输入密码" show-password />
                </el-form-item>
                <el-button type="primary" class="submit-btn" @click="onSubmit" :loading="loading">注册</el-button>
                <div class="login-row">
                    已有账号? <router-link class="link" to="/login">去登录</router-link>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const formRef = ref()
const loading = ref(false)
const form = ref({
    username: '',
    password: '',
    confirmPassword: '',
})

const validateConfirm = (_: any, value: string, callback: (err?: Error) => void) => {
    if (!value) return callback(new Error('请再次输入密码'))
    if (value !== form.value.password) return callback(new Error('两次输入的密码不一致'))
    callback()
}

const rules = {
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码至少 6 位', trigger: 'blur' }
    ],
    confirmPassword: [
        { validator: validateConfirm, trigger: 'blur' }
    ],
}

const onSubmit = async () => {
    await formRef.value?.validate()
    loading.value = true
    try {
        // TODO: 调用实际注册接口
        ElMessage.success('注册成功（示例）')
    } finally {
        loading.value = false
    }
}
</script>

<style scoped>
.register-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f5f7fa;
    background-image: url('@/assets/img/other/backgroud.png');
    background-repeat: no-repeat;
    background-position: center center;
    background-size: auto;
}

.register-card {
    width: 450px;
    max-width: 92vw;
    min-height: 460px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
    padding: 32px 36px 28px;
}

.register-title {
    font-size: 28px;
    font-weight: 800;
    text-align: center;
    margin-bottom: 8px;
    color: #111827;
}

.register-form {
    margin-top: 12px;
}

.submit-btn {
    width: 100%;
    height: 36px;
    margin-top: 4px;
}

.login-row {
    text-align: center;
    margin-top: 16px;
    color: #6b7280;
}

.link {
    color: #3b82f6;
    text-decoration: none;
}

.link:hover,
.link:focus {
    text-decoration: none;
}

@media (max-width: 480px) {
    .register-card {
        padding: 24px 20px 20px;
    }
}

/* 取消浏览器自动填充底色（Chrome/Safari/Edge） */
:deep(input:-webkit-autofill),
:deep(input:-webkit-autofill:hover),
:deep(input:-webkit-autofill:focus),
:deep(input:-webkit-autofill:active) {
    -webkit-text-fill-color: inherit !important;
    transition: background-color 9999s ease-out 0s !important;
    box-shadow: 0 0 0 1000px transparent inset !important;
    caret-color: inherit;
}

/* 兼容新内核伪类，移除自动填充高亮 */
:deep(input:-internal-autofill-selected) {
    appearance: none !important;
    background-image: none !important;
    background-color: transparent !important;
    color: inherit !important;
}
</style>